package com.dongge.action;

import com.dongge.entity.Info;
import com.dongge.service.InfoService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.interceptor.SessionAware;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/10/15.
 */
public class InfoAction extends ActionSupport implements ModelDriven<Info>,SessionAware{

    private Info info = new Info();
    private InfoService infoService;
    private Map<String,Object> session;

    public void setInfoService(InfoService infoService) {
        this.infoService = infoService;
    }

    public String edit(){
        infoService.saveOrUpdate(info);
        return "toList";
    }

    /**
     * 重点处理回显问题
     * @return
     */
    public String editForm(){
        Info info1 = infoService.get(info.getId());
//        info.setId(info1.getId());
//        info.setAge(info1.getAge());
//        info.setName(info1.getName());
        ActionContext.getContext().getValueStack().push(info1);
        return "edit";
    }

    public String add(){

        infoService.saveOrUpdate(info);
        return "toList";
    }

    public String addForm(){
        return "add";
    }

    public String delete(){

        infoService.delete(info.getId());
        return  "toList";
    }

    public String list(){
        List<Info> infos = infoService.getList();
        session.put("list",infos);
        return "list";
    }

    public Info getModel() {
        return info;
    }

    public void setSession(Map<String, Object> session) {
        this.session = session;
    }


}
